<?php
	require_once("lib/includes.php");

    $action = new Action();

    echo json_encode(
        $action->Invoke(function(JSONReply $reply)
        {
            $gameId = (int)$_POST['gameid'];
            $objectId = (int)$_POST['objectid'];
            $objectName = $_POST['objectname'];

            DB::Connect();

            // the first guessed object was wrongly guessed: needs a negative update
            Queries::UpdateObjectsQuestions($gameId, (int)-1, "GuessedObject");

            if ($objectId == -1) // it's a new object
            {
                $objectId = Queries::InsertAndGetNewObject($objectName);
                $guessedRank = Constants::NewObjectGuessedRank;
            }
            else
            {
                $guessedRank = Queries::GetGuessedRank($gameId, $objectId);
            }

            Queries::SetPlayedObject($gameId, $objectId);

            Queries::SetGuessedRank($gameId, $guessedRank);

            Queries::UpdateObjectsQuestions($gameId, (int)1, "PlayedObject");

            $expectedAnswers = Queries::GetExpectedAnswersForGame($gameId, $objectId);

            $reply->SetData($expectedAnswers);

            DB::Disconnect();
        }));
